package com.example.usharingtest.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.usharingtest.R;
import com.example.usharingtest.activities.ArticleViewActivity;
import com.example.usharingtest.model.Article;
import com.example.usharingtest.model.Image;
import com.example.usharingtest.util.ScreenTools;
import com.example.usharingtest.view.CustomImageView;
import com.example.usharingtest.view.NineGridlayout;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Pan_ on 2015/2/3.
 */
public class MainAdapter extends BaseAdapter {
    private Context context;
    private List<List<Image>> datalist;
    private  List<Article> articleList;

    private boolean isZan =false;
    public MainAdapter(Context context, List<List<Image>> datalist, List<Article> articleList) {
        this.context = context;
        this.datalist = datalist;
        this.articleList = articleList;
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        List<Image> itemList = datalist.get(position);
        final Article article = articleList.get(position);
        Log.d("article content",article.getContent());
        //view= LayoutInflater.from(context).inflate(R.layout.userpostsimple,parent,false);


        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.userpostsimple, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ivMore = (NineGridlayout) view.findViewById(R.id.post_image);
            viewHolder.ivOne = (CustomImageView) view.findViewById(R.id.iv_oneimage);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        TextView username=(TextView)view.findViewById(R.id.user_name);
        TextView userposttime=(TextView)view.findViewById(R.id.user_posttime);
        TextView userpostcontent=(TextView)view.findViewById(R.id.post_content);
        TextView zhuan=(TextView)view.findViewById(R.id.repost_total);
        TextView ping=(TextView)view.findViewById(R.id.comment_total);
        TextView zan=(TextView)view.findViewById(R.id.like_total);
        final ImageView zanimage = (ImageView)view.findViewById(R.id.like_image);
        ImageView pingimage = (ImageView)view.findViewById(R.id.comment_image);
        ImageView zhuanimage = (ImageView)view.findViewById(R.id.zhuan_image);
        ImageView image1=(ImageView)view.findViewById(R.id.user_headportrait);

        userpostcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                Intent intent = new Intent(context, ArticleViewActivity.class);
                intent.putExtra("art",article);
                context.startActivity(intent);

            }
        });


        //点赞相应事件--未完成
        zanimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isZan){
                    isZan =false;
                    zanimage.setImageResource(R.drawable.market_icon_dislike);
                }else {
                    isZan =true;
                    zanimage.setImageResource(R.drawable.market_icon_liked);
                }

                zanimage.startAnimation(AnimationUtils.loadAnimation(
                        context, R.anim.dianzan_anim));
            }

        });

        zan.setText(String.valueOf(article.getAgreenum()));

        //评论事件
        ping.setText(String.valueOf(article.getCommentnum()));
        pingimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 调用相应的事件
            }
        });
        //转发事件
        zhuan.setText(String.valueOf(article.getCollectnum()));
        zhuanimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 调用相应的转发事件
            }
        });
        //文章内容填充
        username.setText(article.getUser().getUsername());
        //System.out.println(article.getContent());
        userposttime.setText(article.getPublishtime().toString());
        userpostcontent.setText(article.getContent());
        Picasso.with(view.getContext()).load(context.getResources().getString(R.string.URL_SERVER)+article.getUser().getUserphotopath()).into(image1);
        if (itemList.get(0) == null) {
            viewHolder.ivMore.setVisibility(View.GONE);
            viewHolder.ivOne.setVisibility(View.GONE);
        } else if (itemList.size() == 1) {
            viewHolder.ivMore.setVisibility(View.GONE);
            viewHolder.ivOne.setVisibility(View.GONE);

            handlerOneImage(viewHolder, itemList.get(0));
        } else {
            viewHolder.ivMore.setVisibility(View.VISIBLE);
            viewHolder.ivOne.setVisibility(View.GONE);

            viewHolder.ivMore.setImagesData(itemList);
        }

        return view;
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(context);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = viewHolder.ivOne.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.ivOne.setLayoutParams(layoutparams);
        viewHolder.ivOne.setClickable(true);
        viewHolder.ivOne.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
        viewHolder.ivOne.setImageUrl(image.getUrl());

    }


    class ViewHolder {
        public NineGridlayout ivMore;
        public CustomImageView ivOne;
    }
}
